package test;

public class SleepMonitorExample {
    private static final Object lock = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("Thread 1 acquired lock,并睡眠5秒");
                try {
                    Thread.sleep(5000); // 让线程睡眠5秒钟
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1睡眠结束，并释放了锁。Thread 1 released lock");
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                Thread.sleep(10000); // 让线程睡眠5秒钟
                synchronized (lock) {
                    System.out.println("Thread 2 acquired lock");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });

        t1.start();
        try {
            Thread.sleep(1000); // 让主线程睡眠1秒钟，确保t1线程已经获得了锁
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}

